package com.todod.base.module.base.controller.admin.protocolstandard;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.protocolstandard.vo.*;
import com.todod.base.module.base.dal.dataobject.protocolstandard.ProtocolStandardDO;
import com.todod.base.module.base.service.protocolstandard.ProtocolStandardService;

@Slf4j
@Tag(name = "管理后台 - 协议标准")
@RestController
@RequestMapping("/base/protocol-standard")
@Validated
public class ProtocolStandardController {

    @Resource
    private ProtocolStandardService protocolStandardService;

    @PostMapping("/create")
    @Operation(summary = "创建协议标准")
    public CommonResult<Long> createProtocolStandard(@Valid @RequestBody ProtocolStandardSaveReqVO createReqVO) {
        return success(protocolStandardService.createProtocolStandard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新协议标准")
    public CommonResult<Boolean> updateProtocolStandard(@Valid @RequestBody ProtocolStandardSaveReqVO updateReqVO) {
        protocolStandardService.updateProtocolStandard(updateReqVO);
        return success(true);
    }

    @GetMapping("/enable")
    @Operation(summary = "启用协议标准")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> enableDevice(@RequestParam("id") Long id) {
        log.info("调用启用协议标准控制层");
        protocolStandardService.enable(id);
        return success(true);
    }


    @GetMapping("/disable")
    @Operation(summary = "禁用协议标准")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> disDevice(@RequestParam("id") Long id) {
        log.info("调用禁用协议标准控制层");
        protocolStandardService.disable(id);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除协议标准")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteProtocolStandard(@RequestParam("id") Long id) {
        protocolStandardService.deleteProtocolStandard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得协议标准")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ProtocolStandardRespVO> getProtocolStandard(@RequestParam("id") Long id) {
        ProtocolStandardDO protocolStandard = protocolStandardService.getProtocolStandard(id);
        return success(BeanUtils.toBean(protocolStandard, ProtocolStandardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得协议标准分页")
    public CommonResult<PageResult<ProtocolStandardRespVO>> getProtocolStandardPage(@Valid ProtocolStandardPageReqVO pageReqVO) {
        PageResult<ProtocolStandardDO> pageResult = protocolStandardService.getProtocolStandardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProtocolStandardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出协议标准 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProtocolStandardExcel(@Valid ProtocolStandardPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProtocolStandardDO> list = protocolStandardService.getProtocolStandardPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "协议标准.xls", "数据", ProtocolStandardRespVO.class,
                        BeanUtils.toBean(list, ProtocolStandardRespVO.class));
    }

}